package com.wenli.lt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wenli.lt.entity.Goods;
import com.wenli.lt.service.IGoodsService;
import com.wenli.lt.util.CommonResponse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lt
 * @since 2021-03-23
 */
@RestController
@Api(tags = "商品")
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    IGoodsService iGoodsService;

    @GetMapping("/getGoodsInfo")
    @ApiOperation("获取商品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品Id",required = true),
            @ApiImplicitParam(name = "studentNumber",value = "学生账号",required = true),
    })
    public CommonResponse getGoodsInfo(String goodsId,String studentNumber){
        return new CommonResponse(iGoodsService.getGoodsInfo(goodsId,studentNumber)) ;
    }

    @PostMapping("saveGoods")
    @ApiOperation("发布商品 0服装1美妆2家具3书籍4生活百货5游戏装备6其他")
    public CommonResponse saveGoods(@RequestBody @ApiParam(name = "goods",value = "商品") Goods goods){
        String aBoolean = iGoodsService.saveGoodsInfo(goods);
        if(StringUtils.isBlank(aBoolean)){
            return new CommonResponse(CommonResponse.ERROR_CODE,"数据库错误,请联系管理员!");

        }else {
            if ("1".equals(aBoolean)){
                return new CommonResponse("没有录入发布人!请检查!");

            }else if ("2".equals(aBoolean)){
                return new CommonResponse("该用户不允许发布,请联系管理员!");

            }else {
                return new CommonResponse(aBoolean);
            }
        }

    }

    @PostMapping("removeGoods")
    @ApiOperation("删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品ID")
    })
    public CommonResponse removeGoods(String goodsId){
        Boolean aBoolean = iGoodsService.removeGoods(goodsId);
        if(aBoolean){
            return new CommonResponse("删除成功!");
        }else {
            return new CommonResponse(CommonResponse.ERROR_CODE,"数据出错,请联系管理员");
        }

    }

//    @GetMapping("getMyGoods")
//    @ApiOperation("我发布的商品")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "studentNumber",value = "学号")
//    })
//    public CommonResponse getMyGoods(String studentNumber){
//
//        return null;
//
//    }


    @GetMapping("mangerGoods")
    @ApiOperation("商品管理 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName",value = "商品名字"),
            @ApiImplicitParam(name = "orderWay",value = "排序方式  0 日期  1 收藏数量  2 举报数量"),
            @ApiImplicitParam(name = "classification",value = "0服装1美妆2家具3书籍4生活百货5游戏装备6其他"),
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "页码大小",required = true),
    })
    public  CommonResponse  mangerGoods(String goodsName,Integer orderWay,Integer classification,Long page,Long size){

        return new CommonResponse(iGoodsService.mangerGoods( goodsName, orderWay,classification, page, size));
    }

    @GetMapping("selectGoodsInfo")
    @ApiOperation("商城查询 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName",value = "商品名字"),
            @ApiImplicitParam(name = "classification",value = "0服装1美妆2家具3书籍4生活百货5数码6其他"),
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "页码大小",required = true),
    })
    public  CommonResponse  selectGoodsInfo(String goodsName,Integer classification,Long page,Long size){
        IPage iPage  =  iGoodsService.selectGoodsInfo( goodsName,classification, page, size);
        return new CommonResponse(iPage);
    }

}
